import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { AiDutyStatusRecordsApi } from '#/api/moxi/aiDutyStatusRecords';
// 获取所有部门
import { getDingdDeptInfoPage } from '#/api/moxi/dingddeptinfo';

import Dayjs from 'dayjs';
import { ref } from 'vue';
const deptOptions = ref<Array<{ label: string; value: string }>>([]);
const loadDeptOptions = async () => {
    try {
        const response = await getDingdDeptInfoPage({ pageNo: 1, pageSize: 100 });
        console.log('获取部门');
        console.log(response);
        deptOptions.value = response.list.map(dept => dept.name && dept.deptId ? ({
            label: dept.name,
            value: dept.deptId
        }):[]);
    } catch (error) {
        console.error('加载部门列表失败:', error);
    }
};

loadDeptOptions();

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
    return [
        {
            fieldName: 'name',
            label: '员工姓名',
            component: 'Input',
            componentProps: {
                placeholder: '请输入员工姓名',
                allowClear: true,
            },
        },

        {
            fieldName: 'deptId',
            label: '所属部门',
            component: 'Select',
            componentProps: {
                options: deptOptions,
                placeholder: '请选择部门',
                allowClear: true,
            },
        },
    ];
}

/** 列表的字段 */
export function useGridColumns<T = AiDutyStatusRecordsApi.AiDutyStatusRecords>(
    onStatusChange?: (
        newStatus: number,
        row: T,
    ) => PromiseLike<boolean | undefined>,
): VxeTableGridOptions['columns'] {
    return [
        {
            field: 'id',
            title: '序号',
        },
        {
            field: 'name',
            title: '员工姓名',
            formatter: ({ row }) => {
                return row ? row.employee.name : '';
            },
        },
        // {
        //     field: 'userid',
        //     title: '员工Id'
        // },
        {
            field: 'deptNames',
            title: '所属部门 ',
            formatter: ({ row }) => {
                return row?.employee?.deptNames?.split(',').pop()?.trim() || '';
            },
        },
        {
            field: 'startTime',
            title: '开始时间',
            formatter: ({ cellValue }) => {
                return cellValue ? Dayjs(cellValue).format('YYYY-MM-DD HH:mm') : '';
            },
        },
        {
            field: 'endTime',
            title: '结束时间',
            formatter: ({ cellValue }) => {
                return cellValue ? Dayjs(cellValue).format('YYYY-MM-DD HH:mm') : '';
            },
        },
        {
            field: 'falseAlarmDesc',
            title: '误报描述'
        },
        // {
        //     field: 'deptName',
        //     title: '触发离岗规则',
        // },
        // {
        //     title: '操作',
        //     width: 180,
        //     fixed: 'right',
        //     slots: { default: 'actions' },
        // },
    ];
}
